package com.vonzhou.springinaction.springidol;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

/**
 * @author XiaoDongLei
 * @Package com.vonzhou.springinaction.springidol
 * @date 2018/3/27
 * @Description: 使用注解创建切面
 * 创建环绕通知
 *
 * 环绕通知是最为强大的通知类型。它能够让你所编写的逻辑将被通知的目标方法完全包装起来。实际上就像在一个通知方法中同时编写前置通知和后置通知。通过使用一个环绕通知来代替之前多个不同的前置通知和后置通知。
 */
@Aspect
public class AroundAudience {

	@Pointcut("execution(* com.vonzhou.springinaction.springidol.Performer.perform(..))")
	public void performance() {
	}

	@Around("performance()")
	public void watchPerformance(ProceedingJoinPoint joinpoint) {
		try {
			System.out.println("The audience is taking their seats.");
			System.out.println("The audience is turning off their cellphones");

			long start = System.currentTimeMillis();
			joinpoint.proceed();
			long end = System.currentTimeMillis();

			System.out.println("CLAP CLAP CLAP CLAP CLAP");

			System.out.println("The performance took " + (end - start)
					+ " milliseconds.");
		} catch (Throwable t) {
			System.out.println("Boo! We want our money back!");
		}
	}
}
